package com.symone.webcms.controller.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.ScheduleTask;
import com.symone.webcms.service.*;
import com.xiaoleilu.hutool.lang.Validator;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/cms/chart")
public class ChartController extends BaseController {

    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private UserLogService userLogService;
    @Autowired
    private ScheduleTaskService taskService;

    @RequiresPermissions("chart:loginLog:index")
    @RequestMapping(value = "/loginlog/index", method = RequestMethod.GET)
    public ModelAndView loginLog(String loginName) {
        ModelAndView mv = new ModelAndView();
        List<String> months = loginLogService.getDistinctYmByCreateTime(loginName);
        mv.setViewName("/chart/loginlog/index");
        mv.addObject("months",months);
        return mv;
    }

    @RequiresPermissions("chart:sysLog:index")
    @RequestMapping(value = "/syslog/index", method = RequestMethod.GET)
    public ModelAndView sysLog(String userName) {
        ModelAndView mv = new ModelAndView();
        List<String> months = userLogService.getDistinctYmByCreateTime(userName);
        mv.setViewName("/chart/syslog/index");
        mv.addObject("months",months);
        return mv;
    }

    @RequiresPermissions("chart:taskLog:index")
    @RequestMapping(value = "/tasklog/index", method = RequestMethod.GET)
    public ModelAndView taskLog(Long tid) {
        Wrapper<ScheduleTask> ew = new EntityWrapper<ScheduleTask>();
        ew.where("1=1");
        List<ScheduleTask> tasks = taskService.selectList(ew);

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/chart/tasklog/index");
        mv.addObject("tasks",tasks);
        mv.addObject("tid",tid);
        return mv;
    }
}
